package com.jwds.digital.bios.business.service.service.dispatch.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jwds.digital.bios.business.core.exceptions.RecordNotFoundException;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.DispatchRuleReqDTO;
import com.jwds.digital.bios.business.core.infrastructure.dto.dispatch.assembler.DispatchRuleAssembler;
import com.jwds.digital.bios.business.core.infrastructure.entity.dispatch.DispatchRule;
import com.jwds.digital.bios.business.core.infrastructure.mapper.dispatch.DispatchRuleMapper;
import com.jwds.digital.bios.business.core.infrastructure.vo.dispatch.DispatchRuleRespVO;
import com.jwds.digital.bios.business.service.service.dispatch.IDispatchRuleService;
import com.jwds.tdc.framework.application.lib.page.PageRequest;
import com.jwds.tdc.framework.application.lib.page.PageResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 规则内容实现类
 *
 * @author 林柏云
 * @date 2024-10-24
 */
@Slf4j
@Service
public class DispatchRuleServiceImpl extends ServiceImpl<DispatchRuleMapper, DispatchRule> implements IDispatchRuleService {

    @Autowired
    private DispatchRuleMapper baseMapper;

    @Override
    public DispatchRuleRespVO queryById(Long id) {
        if (id == null) {
            return null;
        }
        DispatchRule entity = baseMapper.selectById(id);
        return DispatchRuleAssembler.entity2dto(entity);
    }

    @Override
    public PageResponse<DispatchRuleRespVO> page(PageRequest<DispatchRuleReqDTO> request) {
        DispatchRuleReqDTO condition = request.getQueryCondition();
        Page<DispatchRuleRespVO> page = new Page<>(request.getPageIndex(), request.getPageSize());
        IPage<DispatchRuleRespVO> pageData = baseMapper.pageQuery(page, condition);
        return new PageResponse<>((Page<?>) pageData, pageData.getRecords());
    }


    @Override
    public void add(DispatchRuleReqDTO reqDto) {
        List<DispatchRule> entityList = baseMapper.selectList(null);
        for (DispatchRule entity : entityList) {
            if (entity.getRuleCode().equals(reqDto.getRuleCode())) {
                throw new RuntimeException("规则编码重复");
            } else if (entity.getRuleCode().equals(reqDto.getRuleName())) {
                throw new RuntimeException("规则名称重复");
            }
        }
        DispatchRule entity1 = DispatchRuleAssembler.dto2entity(reqDto);
        baseMapper.insert(entity1);
    }


    @Override
    public void update(DispatchRuleReqDTO reqDto) {
        DispatchRule oldEntity = baseMapper.selectById(reqDto.getId());
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        if (oldEntity.getRuleCode().equals(reqDto.getRuleCode())) {
            throw new RuntimeException("规则编码重复");
        } else if (oldEntity.getRuleName().equals(reqDto.getRuleName())) {
            throw new RuntimeException("规则名称重复");
        }
        DispatchRule newEntity = DispatchRuleAssembler.dto2entity(reqDto, oldEntity);
        baseMapper.updateById(newEntity);
    }

    @Override
    public void delete(Long id) {
        DispatchRule oldEntity = baseMapper.selectById(id);
        if (oldEntity == null) {
            throw new RecordNotFoundException();
        }
        baseMapper.deleteById(id);
    }


}
